package com.blacksea.authserver.web.configuration;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;
import java.util.TimeZone;

/**
 * WebConfigSupport
 * 配置WebMvcConfigurationSupport后 导致不扫描WebMvcConfigurer, 故去掉此配置类,功能迁移过去
 *
 * @author blacksea3(jxt) 2021/4/11 17:14
 */
//@Configuration
public class WebConfigSupport extends WebMvcConfigurationSupport {
    // /**
    //  * 统一序列化风格: 针对MappingJackson2HttpMessageConverter, POJO与json转换
    //  * See {@link com.fasterxml.jackson.databind.PropertyNamingStrategy.SnakeCaseStrategy} for details.
    //  * @param converters .
    //  */
    // @Override
    // public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    //     for (int i = 0; i < converters.size(); i++) {
    //         if (converters.get(i) instanceof MappingJackson2HttpMessageConverter) {
    //             ObjectMapper objectMapper = new ObjectMapper();
    //             // 修改Json 序列化与反序列化的策略：小写字母下划线策略
    //             objectMapper.setPropertyNamingStrategy(new PropertyNamingStrategy.SnakeCaseStrategy());
    //             // 属性为null的不参与序列化与反序列化
    //             objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    //             // 出现未知属性(field)时抛出异常JsonMappingException
    //             objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    //             // 时区修改
    //             objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    //             // 新的MappingJackson2HttpMessageConverter构造
    //             MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    //             converter.setObjectMapper(objectMapper);
    //             // 替换MappingJackson2HttpMessageConverter
    //             converters.set(i, converter);
    //             break;
    //         }
    //     }
    // }
}
